package com.example.demo.school.controllers;


import com.example.demo.school.controllers.form.StudentForm;
import com.example.demo.school.models.Student;
import com.example.demo.school.models.StudentType;
import com.example.demo.school.models.search.StudentSearch;
import com.example.demo.school.repositories.StudentRepository;
import com.example.demo.school.repositories.StudentTypeRepository;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping(value = "/students")
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private StudentTypeRepository typeRepository;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(StudentSearch search, ModelMap map) {
        List<Student> list = studentRepository.findBySearch(search);
        map.put("list", list);
        map.put("search", search);
        return "/student/index";
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        StudentForm form = StudentForm.of(null);
        map.put("form", form);
        return "/student/add";
    }


    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap map) throws Exception {
        Student student = checkEntity(studentRepository.findOptional(id));
        map.put("form", StudentForm.of(student));
        map.put("id", id);
        return "/student/edit";
    }

    @Transactional
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String create(@ModelAttribute(name = "form") StudentForm form, final RedirectAttributes flash) throws Exception {
        StudentType type = null;
        if (form.getTypeId() != null) {
            type = this.checkEntity(typeRepository.findOptional(form.getTypeId()));
        }
        Student student = form.toStudent(null, type);
        studentRepository.save(student);
        flash.addFlashAttribute("flashSuccess", "数据保存成功");
        return "redirect:/students";
    }

    @Transactional
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(@PathVariable Long id, @ModelAttribute(name = "form") StudentForm form, final RedirectAttributes flash) throws Exception {
        StudentType type = null;
        Student student = this.checkEntity(studentRepository.findOptional(id));
        if (form.getTypeId() != null) {
            type = this.checkEntity(typeRepository.findOptional(form.getTypeId()));
        }
        form.toStudent(student, type);
        studentRepository.save(student);
        flash.addFlashAttribute("flashSuccess", "数据保存成功");
        return "redirect:/students";
    }

    @Transactional
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable Long id) throws Exception {
        Student student = this.checkEntity(studentRepository.findOptional(id));
        studentRepository.delete(student);
        return "redirect:/students";
    }

    protected <T> T checkEntity(Optional<T> optional) throws NotFoundException {
        if (!optional.isPresent()) {
            throw new NotFoundException("所查询的记录无效");
        }
        return optional.get();
    }
}
